use egui::{Context, Style, Visuals, Color32, Rounding, Stroke, Vec2};

// 主题颜色
pub struct AppTheme {
    pub primary: Color32,
    pub secondary: Color32,
    pub accent: Color32,
    pub background: Color32,
    pub card_background: Color32,
    pub panel_background: Color32,
    pub text: Color32,
    pub text_secondary: Color32,
    pub success: Color32,
    pub warning: Color32,
    pub error: Color32,
}

impl Default for AppTheme {
    fn default() -> Self {
        // 默认是暗色主题
        Self::dark_theme()
    }
}

impl AppTheme {
    pub fn dark_theme() -> Self {
        Self {
            primary: Color32::from_rgb(0, 150, 136),      // Teal
            secondary: Color32::from_rgb(63, 81, 181),    // Indigo
            accent: Color32::from_rgb(255, 193, 7),       // Amber
            background: Color32::from_rgb(18, 18, 18),    // 暗色背景
            card_background: Color32::from_rgb(33, 33, 33), // 卡片背景
            panel_background: Color32::from_rgb(29, 29, 29), // 面板背景
            text: Color32::from_rgb(255, 255, 255),       // 白色文本
            text_secondary: Color32::from_rgb(170, 170, 170), // 次要文本
            success: Color32::from_rgb(76, 175, 80),      // Green
            warning: Color32::from_rgb(255, 152, 0),      // Orange 
            error: Color32::from_rgb(244, 67, 54),        // Red
        }
    }
    
    pub fn light_theme() -> Self {
        Self {
            primary: Color32::from_rgb(0, 150, 136),      // Teal
            secondary: Color32::from_rgb(63, 81, 181),    // Indigo  
            accent: Color32::from_rgb(255, 152, 0),       // Orange
            background: Color32::from_rgb(245, 245, 245), // 浅灰背景
            card_background: Color32::from_rgb(255, 255, 255), // 白色卡片
            panel_background: Color32::from_rgb(240, 240, 240), // 面板背景
            text: Color32::from_rgb(33, 33, 33),          // 近黑文本
            text_secondary: Color32::from_rgb(117, 117, 117), // 次要文本
            success: Color32::from_rgb(76, 175, 80),      // Green
            warning: Color32::from_rgb(255, 152, 0),      // Orange 
            error: Color32::from_rgb(244, 67, 54),        // Red
        }
    }
    
    pub fn custom_theme(primary: (u8, u8, u8), secondary: (u8, u8, u8), is_dark: bool) -> Self {
        let mut theme = if is_dark { Self::dark_theme() } else { Self::light_theme() };
        theme.primary = Color32::from_rgb(primary.0, primary.1, primary.2);
        theme.secondary = Color32::from_rgb(secondary.0, secondary.1, secondary.2);
        theme
    }
    
    // 应用主题到egui上下文
    pub fn apply_to_ctx(&self, ctx: &Context) {
        let mut style = (*ctx.style()).clone();
        
        // 设置视觉样式
        let mut visuals = if self.background.r() < 100 {
            Visuals::dark()
        } else {
            Visuals::light()
        };
        
        // 自定义视觉样式
        visuals.override_text_color = Some(self.text);
        visuals.window_fill = self.card_background;
        visuals.panel_fill = self.panel_background;
        visuals.window_stroke = Stroke::new(1.0, self.secondary.gamma_multiply(0.5));
        
        // 按钮和其他交互元素样式
        visuals.widgets.inactive.bg_fill = self.card_background;
        visuals.widgets.inactive.fg_stroke = Stroke::new(1.0, self.text);
        visuals.widgets.hovered.bg_fill = self.card_background.gamma_multiply(1.2);
        visuals.widgets.hovered.fg_stroke = Stroke::new(1.5, self.primary);
        visuals.widgets.active.bg_fill = self.primary;
        visuals.widgets.active.fg_stroke = Stroke::new(2.0, Color32::WHITE);
        
        // 设置圆角
        visuals.window_rounding = Rounding::same(10.0);
        visuals.menu_rounding = Rounding::same(6.0);
        visuals.button_frame = true;
        
        style.visuals = visuals;
        ctx.set_style(style);
    }
}

// UI元素尺寸常量
pub struct AppSizes {
    pub nav_bar_width: f32,
    pub nav_icon_size: Vec2,
    pub card_margin: f32,
    pub card_rounding: Rounding,
    pub button_height: f32,
    pub button_rounding: Rounding,
    pub heading_size: f32,
    pub spacing: f32,
}

impl Default for AppSizes {
    fn default() -> Self {
        Self {
            nav_bar_width: 180.0,
            nav_icon_size: Vec2::new(24.0, 24.0),
            card_margin: 16.0,
            card_rounding: Rounding::same(12.0),
            button_height: 36.0,
            button_rounding: Rounding::same(6.0),
            heading_size: 24.0,
            spacing: 10.0,
        }
    }
} 